package org.elip.stewiemaze.server.servlets;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.elip.stewiemaze.server.DatastoreHelper;
import org.elip.stewiemaze.server.SignedRequestDecoder;
import org.elip.stewiemaze.shared.entities.Player;

public class FacebookDeauthenticateServlet extends HttpServlet {
	
	private Logger logger = Logger.getLogger(FacebookDeauthenticateServlet.class.getName());

	/**
	 * 
	 */
	private static final long serialVersionUID = -5639657768869094122L;
	
	@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        handle(req, resp);
    }
    

	@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        handle(req, resp);
    }


	@SuppressWarnings("unchecked")
	private void handle(HttpServletRequest req, HttpServletResponse resp) {
		
		String input = (String)req.getParameter("signed_request").toString();
		
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		
		try {
			jsonMap = SignedRequestDecoder.parse_signed_request(input, "f493dbd66eb6d4e668f17628ae52c514");
		} catch (Exception e1) {
		}
		
		
		String userId = (String)jsonMap.get("user_id");
		
		logger.fine("user id for deauthorization is " + userId);
		logger.fine("deleting user with user id " + userId + " from database");
		DatastoreHelper.deletePlayer(userId);
		
	}
}
